<?php
/**
 * Description of QnItemclass
 *
 * @author Chisheng Lu
 * @copyright DoRISE team, Science Education center, NTNU.
 * @since 2010-05-03
 * @version 1.0
 */
class QnItem {
//put your code here
    private $mysqli;

    function __construct() {
    //引入資料庫連結參數設定
        include '../inc/oaConn.inc.php';
        $this->mysqli = @new mysqli($mysqlHost,$mysqlUser,$mysqlPassword,$mysqlDB);
        if(mysqli_connect_errno()) {
            throw new Exception('無法建立資料庫連線！'.mysqli_connect_error());
            $this->mysqli = false;
            exit();
        }

        //連線使用編碼
        $this->mysqli->query("SET NAMES 'utf8'");
    }

    /**
     *
     * @param Array $aryQnItem
     * @return Boolean
     */
    public function addQnItem($aryQnItem) {
        $bolRtn = false;
        $strNow = $this->getNowTime();
        $sql_addQI = 'INSERT INTO `QnItem` (';
        $sql_values = ' VALUES(';
        if (array_key_exists('QnITitle', $aryQnItem)) {
            $sql_addQI .= 'QnITitle';
            $sql_values .= '\'' . $aryQnItem['QnITitle'] . '\'';
        }
        if (array_key_exists('QnIType', $aryQnItem)) {
            $sql_addQI .= ', QnIType ';
            $sql_values .= ', \'' . $aryQnItem['QnIType'] . '\'';
        }
        if (array_key_exists('QnIContent', $aryQnItem)) {
            $sql_addQI .= ', QnIContent ';
            $sql_values .= ', \'' . $aryQnItem['QnIContent'] . '\'';
        }
        $sql_addQI .= ')' . $sql_values . ');';
        //echo "sql_addQI:$sql_addQI<br>";
        $rs_addQI = $this->mysqli->query($sql_addQI);
        if (!$rs_addQI) {
            throw new Exception('新增問卷項目時發生錯誤！');
        }else {
            $bolRtn = true;
        }
        return $bolRtn;

    }

    /**
     *
     * @param Integer $intQnIId
     * @param Array $aryQnItem
     * @return Boolean
     */
    public function editQnItem($intQnIId, $aryQnItem) {
    //檢查問卷項目編號是否為空白
        if (empty ($intQnIId)) {
            throw new Exception('缺少問卷項目編號！');
        }
        $bolRtn = false;
        $strNow = $this->getNowTime();
        $sql_editQI = 'Update QnItem set ';
        if (array_key_exists('QnITitle', $aryQnItem))
            $sql_editQI .= 'QnITitle = \'' . $aryQnItem['QnITitle'] . '\'';
        if (array_key_exists('QnIType', $aryQnItem))
            $sql_editQI .= ', QnIType = \'' . $aryQnItem['QnIType'] . '\'';
        if (array_key_exists('QnIContent', $aryQnItem))
            $sql_editQI .= ', QnIContent = \'' . $aryQnItem['QnIContent'] . '\' ';
        $sql_editQI .= ' where QnIId = ' . $intQnIId . ';';
        echo "sql_editQI:$sql_editQI<br>";
        $rs_editQI = $this->mysqli->query($sql_editQI);
        if (!$rs_editQI) {
            throw new Exception('更新問卷項目時發生錯誤！');
        }else {
            $bolRtn = true;
        }
        return $bolRtn;
    }

    /**
     *
     * @param Integer $intQnIId
     * @return Boolean
     */
    public function delQnItem($intQnIId) {
        $bolRtn = false;
        //檢查問卷項目編號是否為空白
        if (empty ($intQnIId)) {
            throw new Exception('錯誤！缺少問卷項目編號。');
        }

        //刪除
        $sql_delQI = 'DELETE FROM `QnItem` WHERE `QnIId`=' . $intQnIId;
        $rs_delQI = $this->mysqli->query($sql_delQI);
        if (!$rs_delQI) {
            throw new Exception('錯誤！刪除問卷項目時發生錯誤。');
        }else {
            $bolRtn = true;
        }

        return $bolRtn;

    }

    /**
     *
     * @param Integer $intQnIId
     * @return Array
     */
    public function getQnItemInfoByQnIId($intQnIId) {
    //檢查問卷項目編號是否為空白
        if (empty ($intQnIId)) {
            throw new Exception('缺少問卷項目編號！');
        }

        $sql_getQIById = 'SELECT * FROM `QnItem` WHERE `QnIId`=' . $intQnIId;
        $rs_getQIById = $this->mysqli->query($sql_getQIById);
        if (!$rs_getQIById) {
            throw new Exception('無法查詢該問卷項目的資料！');
        }else {
            $row_getQIById = $rs_getQIById->fetch_row();
            return $row_getQIById;
        } //End If

    }

    /**
     *
     * @return Array Two dimension array
     */
    public function getQnItemList() {
        $sql_getQIList = 'SELECT * FROM `QnItem` ORDER BY `QnIId`';
        $rs_getQIList = $this->mysqli->query($sql_getQIList);

        if (!$rs_getQIList) {
            throw new Exception('無法查詢該問卷項目的資料！');
        }else {
            $aryQIList = array();
            while ($aryOneQI = $rs_getQIList->fetch_row()) {
                array_push($aryQIList, $aryOneQI);
            }
            return $aryQIList;
        } //End If
    }

    /**
     *
     * @return String
     */
    private function getNowTime() {
        return date('Y-m-d H:i:s');
    }

    function  __destruct() {
        if ($this->mysqli) {
            $this->mysqli->close();
            $this->mysqli = FALSE;
        }
    }
}
?>
